package cn.com.headfree.util;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.type.TypeReference;

import cn.com.headfree.entity.isc.LongSerializer;
import cn.com.headfree.entity.isc.Paging;
import cn.com.headfree.entity.isc.RestServerResult;
import cn.com.headfree.entity.isc.TimestampSerializer;

/**  
 * @Title  JsonUtil.java   
 * @Package cn.com.headfree.util   
 * @Description TODO
 * @author yangwang 
 * @date  2020年8月4日 下午5:39:22   
 */
public class JsonUtil {
	private static JsonUtil jsonUtil;
	  private ObjectMapper mapper;

	  private JsonUtil(ObjectMapper mapper)
	  {
	    this.mapper = mapper;
	  }

	  private static JsonUtil getInstance() {
	    if (jsonUtil == null) {
	      ObjectMapper mapper = new ObjectMapper();
	      SimpleModule module = new SimpleModule("JsonUtil", 
	        new Version(1, 0, 0, null));
	      module.addSerializer(new LongSerializer());
	      module.addSerializer(new TimestampSerializer());
	      mapper.registerModule(module);
	      jsonUtil = new JsonUtil(mapper);
	    }
	    return jsonUtil;
	  }

	  private ObjectMapper getMapper() {
	    return this.mapper;
	  }

	  public static <T> T jsonToObject(String json, Class<T> clazz) throws Exception {
	    return getInstance().getMapper().readValue(json, clazz);
	  }

	  public static String toJson(Object value) throws Exception {
	    return getInstance().getMapper().writeValueAsString(value);
	  }

	  public static <T> T covertValue(Object value, Class<T> toValueType) throws Exception {
	    return getInstance().getMapper().convertValue(value, toValueType);
	  }

	  public static ArrayList<?> toObjectArrayListFromJson(String json, TypeReference<?> t) throws JsonParseException, JsonMappingException, IOException {
	    return (ArrayList)getInstance().getMapper().readValue(json, t);
	  }

	  public static Paging toPaging(RestServerResult result, Class clazz) throws Exception {
	    Paging page = (Paging)toObject((LinkedHashMap)result.getData(), Paging.class);
	    List data = (List)page.getData();
	    result.setData(data);
	    List funcs = toList(result, clazz);
	    page.setData(funcs);
	    return page;
	  }

	  public static <T> T convertValue(Object fromValue, TypeReference<T> type) {
	    return getInstance().getMapper().convertValue(fromValue, type);
	  }

	  public static <T> T toObject(LinkedHashMap<?, ?> link, Class<T> clazz) throws Exception {
		  return covertValue(link, clazz);
	  }

	  public static <T> List<T> toList(RestServerResult result, Class<T> clazz) throws Exception {
	    List perms = new ArrayList();
	    List<LinkedHashMap> list = (List)result.getData();
	    Method me = getMethod("setResExt", clazz);
	    for (LinkedHashMap obj : list) {
	      LinkedHashMap resExt = (LinkedHashMap)obj.get("resExt");
	      Object t = covertValue(obj, clazz);
	      perms.add(t);
	    }
	    return perms;
	  }

	  private static Method getMethod(String fieldName, Class<?> clazz) {
	    Method me = null;
	    try {
	      me = clazz.getMethod(fieldName, new Class[] { Map.class });
	      if (me != null)
	        me.setAccessible(true);
	    }
	    catch (Exception localException) {
	    }
	    return me;
	  }
}
